package com.solidumgame.martillo.health
{
	import com.pblabs.engine.PBE;
	import com.pblabs.engine.entity.EntityComponent;
	import com.solidumgame.martillo.hammer.events.HammerGenericEvent;
	import com.solidumgame.martillo.health.events.HealthGenericEvent;
	import com.solidumgame.martillo.nail.events.NailGenericEvent;
	
	import flash.events.Event;
	
	public class HealthLogicComponent extends EntityComponent
	{	
		public var health:Number;
		public var maxHealth:Number;
		public var minHealth:Number;
		
		public function HealthLogicComponent()
		{
			super();
			this.maxHealth = 100;
			this.minHealth = 0;
			this.health = 100;
		}
	
		override protected function onReset():void
		{	
			super.onReset();
			PBE.mainStage.addEventListener(NailGenericEvent.NAIL_NOT_NAILED_EVENT,floorHit);
			PBE.mainStage.addEventListener(HammerGenericEvent.FLOOR_HIT_EVENT,floorHit);
			PBE.mainStage.addEventListener(NailGenericEvent.NAIL_NAILED_EVENT,nailNailed);
		}
		
		override protected function onRemove():void
		{	
			super.onRemove();
			PBE.mainStage.removeEventListener(NailGenericEvent.NAIL_NOT_NAILED_EVENT,floorHit);
			PBE.mainStage.removeEventListener(HammerGenericEvent.FLOOR_HIT_EVENT,floorHit);
			PBE.mainStage.removeEventListener(NailGenericEvent.NAIL_NAILED_EVENT,nailNailed);
		}
		
		protected function nailNotNailed(e:Event):void
		{
				
		
		
		}
		
		protected function floorHit(e:Event):void
		{	
			damage(10);
			owner.eventDispatcher.dispatchEvent(new HealthGenericEvent(HealthGenericEvent.DAMAGE_EVENT,health));
			if(health<=minHealth)
				PBE.mainStage.dispatchEvent(new HealthGenericEvent(HealthGenericEvent.HEALTH_ZERO_EVENT,0));
		}
		
		protected function nailNailed(e:Event):void
		{
			heal(10);
			owner.eventDispatcher.dispatchEvent(new HealthGenericEvent(HealthGenericEvent.UP_HEALTH_EVENT,health));
		}
		
		protected function heal(amount:Number):void
		{	
			if(health<maxHealth)
				health += amount;
			
			if(health>maxHealth)
				health = maxHealth;
		}	
		
		protected function damage(amount:Number):void
		{	
			if(health>minHealth)
				health -= amount;
			
			if(health<minHealth)
				health = minHealth;
		}
		
		public function getHealth():Number
		{
			return health;
		}
	
	}
}